<?php
class User extends AppModel {

	var $name = 'User';	
	var $validate = array(
		'email' => array(
			'email_rule1' => array(
				'rule' => 'email',
			    'required'      => true,  
				'allowEmpty'    => false,  
				'message' => 'Must input a valid email address.'),
			'email_rule2' => array(
				'rule' => 'notEmpty',
			    'required'      => true,  
				'allowEmpty'    => false,  
				'message' => 'Must input a valid email address.'),
			'email_rule3' => array(
				'rule' => 'isUnique',  
				'message' => 'Email address already taken.')
		),
		'username' => array(
			'name_rule1' => array(
				'rule' => array('custom', '/^[a-zA-Z0-9_]{3,16}$/'),
				'message' => 'Username can only contain letters, numbers, and _. It must be atleast 3 characters and at most 16.'),
			'name_rule2' => array(
				'rule' => 'isUnique',
				'message' => 'Username already taken.')
		),
		'verify_password' => array(
			'rule' => array('minLength', 6), 
			'required'      => true,  
			'allowEmpty'    => false,)
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasOne = array(
		'Profile' => array(
			'className' => 'Profile',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'Story' => array(
			'className' => 'Story',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Rating' => array(
			'className' => 'Rating',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

	var $hasAndBelongsToMany = array(
 		'Friend' =>
 			array(
				 'className' => 'Friend',
				 'joinTable' => 'friends',
				 'foreignKey' => 'friend_id',
				 'associationForeignKey' => 'user_id',
 				 'with' => 'Friend',
				 'unique' => true,
				 'conditions' => '',
				 'fields' => '',
				 'order' => '',
				 'limit' => '',
				 'offset' => '',
				 'finderQuery' => '',
				 'deleteQuery' => '',
				 'insertQuery' => ''
 				)
 		);
 		
 		
 	
 	
 	function findFriends($id = null)
 	{
 		if($id == null)
 		 return null;

		 $this->Friend->Profile->unbindModel(array('hasMany' => array('Post')));
 		 $this->Friend->recursive = 2;
 		 $friends = $this->Friend->find('all',array(
 		 	'conditions' => array('Friend.user_id' => $id)
 		 ));
 		 return $friends;
 	}

 	
}
?>